package com.fxu;

import cn.hutool.core.util.StrUtil;
import com.fxu.framework.biz.holder.UploadHolder;
import com.fxu.framework.core.base.Constant;
import com.fxu.framework.core.base.Result;
import com.fxu.framework.core.holder.RequestHolder;
import com.fxu.framework.core.sql.SEntity;
import com.fxu.framework.core.sql.SQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.Utils;
import org.web3j.abi.datatypes.*;
import org.web3j.abi.datatypes.generated.Uint256;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;

@Data
@RestController
@RequestMapping("/api/demo")
@Api(tags = "演示接口")
public class DemoController extends SEntity<DemoController> {

    @Value("${logging.pattern.console:}")
    private String demo;

    @PostMapping("/console/")
    @ApiOperation("获得日志输出格式")
    public String console() {
        return demo;
    }


    @PostMapping("/test/")
    @ApiOperation("测试web3j的返回数据")
    public String test(@RequestParam String content) {
//        Type back = FunctionReturnDecoder.decodeIndexedValue(content, new TypeReference<Utf8String>() {
//        });
//        System.out.println(back);

//        org.web3j.abi.datatypes.Function function = new Function(
//                "transfer",
//                Arrays.asList(),
//                Arrays.asList(
//                        new TypeReference<Address>() {
//                        },
//                        new TypeReference<Uint256>() {
//                        }
//                )
//        );
//        String data = content.substring(0, 9) + content.substring(17);
//        List<Type> params = FunctionReturnDecoder.decode(data, Utils.convert(Arrays.asList(
//                new TypeReference<Utf8String>() {
//                },
//                new TypeReference<Uint256>() {
//                }
//        )));
//        for (Type param : params) {
//            System.out.println(param.getValue().toString());
//        }
//        String orderId = params.get(0).getValue().toString();
//        String albumId = params.get(1).getValue().toString();

        return getOrderIdByBlockReceiptData(content);
    }

    public String getOrderIdByBlockReceiptData(String input) {
        String data = input.substring(0, 9) + input.substring(17);
        List<Type> params = FunctionReturnDecoder.decode(data, Utils.convert(Arrays.asList(
                new TypeReference<Utf8String>() {
                }
        )));
        return params != null && params.get(0) != null && params.get(0).getValue() != null ? params.get(0).getValue().toString() : "";
    }

}